package com.zsrt.devbase.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.zsrt.devbase.common.annotation.Paging;
import com.zsrt.devbase.common.utils.ConstantDefine;
import com.zsrt.devbase.dao.FunctionInfoDAO;
import com.zsrt.devbase.dto.FunctionInfoDTO;
import com.zsrt.devbase.service.FunctionInfoService;

/** 
 * @author leixin  Email:leixin821792669@gmail.com
 * @date 2017年5月14日 上午1:47:45
 * @version 1.0
 */
@Service
public class FunctionInfoServiceImpl extends BaseService<FunctionInfoDTO> implements FunctionInfoService{

	@Autowired
	private FunctionInfoDAO functionInfoDAO;
	@Override
	public Map<String, Object> check(FunctionInfoDTO entity) {
		// TODO Auto-generated method stub
		return null;
	}

	@Paging
	@Override
	public Map<String, Object> findByEntity(FunctionInfoDTO entity) {
		Map<String, Object> result = new HashMap<>();
		List<FunctionInfoDTO> select = baseDAO.select(entity);
		result.put(ConstantDefine.ROWS, select);
		return result;
	}

	@Override
	public List<Object> getFunctionListByRoleList(List<String> roleList) {
		List<FunctionInfoDTO> functions = functionInfoDAO.getFunctionListByRoleList(roleList);
		List<Object> list = new ArrayList<>();
		if (CollectionUtils.isEmpty(functions)) {
			return list;
		}
		List<Map<String, Object>> resultList = new ArrayList<>();
		Map<String, Object> resultMap = new HashMap<>();
		String tempParentCode = null; 
		for (FunctionInfoDTO func : functions) {
			if (null == tempParentCode) {
				tempParentCode = func.getParentFunctionCode();
			}
			if (!tempParentCode.equals(func.getParentFunctionCode())) {
				//如果父节点不同，就新增一个
				resultMap.put("parentCode",tempParentCode);
				resultMap.put("treeData", resultList);
				list.add(resultMap);
				resultMap = new HashMap<>();
				resultList = new ArrayList<>();
				tempParentCode = func.getParentFunctionCode();
			}
			Map<String, Object> result = new HashMap<>();
			Map<String, String> attr = new HashMap<>();
			result.put("id", func.getId());
			result.put("text", func.getFunctionName());
			attr.put("url", func.getFunctionUrl());
			result.put("attributes", attr);
			resultList.add(result);
		}
		if (!resultList.isEmpty()) {
			resultMap.put("parentCode", tempParentCode);
			resultMap.put("treeData", resultList);
			list.add(resultMap);
		}
		return list;
	}

	@Override
	public List<FunctionInfoDTO> getFunctionAllByRoleList(List<String> roleList) {
		
		return functionInfoDAO.getFunctionAllByRoleList(roleList);
	}

}
